/*
 * Copyright [2011] [Henry and Will]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */

package org.hnw.wte;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class AddRestActivity extends Activity {
//	private RelativeLayout layout;
	private EditText restNameEditText;
	private String restName;
	private Button addRestButton;
	private AlertDialog alertDialog;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        
        setContentView(R.layout.add_rest);
        
        restNameEditText = (EditText)this.findViewById(R.id.editText1);
        restNameEditText.addTextChangedListener(new TextWatcher()
        	{
	        	public void afterTextChanged(Editable s)
				{
	        		restName = restNameEditText.getText().toString();
				}
				
				public void beforeTextChanged(CharSequence s, int start, int count, int after)
				{
					// TODO Auto-generated method stub
					
				}
				
				public void onTextChanged(CharSequence s, int start, int before, int count)
				{
					// TODO Auto-generated method stub
					
				}
        	});
        addRestButton = (Button) this.findViewById(R.id.AddRestButton);
        alertDialog = new AlertDialog.Builder(this).create();
        addRestButton.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				
				// save to database
				Restaurant rest = new Restaurant(restName, null, null, null);
				saveToDB(rest);
			}
			
		});
    }
    private void saveToDB(Restaurant rest) {
    	wteDataBase db = new wteDataBase(this);
    	db.DBopen();
    	db.DBinsert(rest);
    	alertDialog.setMessage(restName + " saved.");
		alertDialog.setButton("OK", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				return;
			}
		});
		alertDialog.show();
    	return;
    }
    
}
